package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonUtils.Result;
import com.atguigu.commonUtils.ResultUtil;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.util.AliyunVodSDKUtils;
import com.atguigu.vod.util.ConstantPropertiesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "视频点播")
@RestController
@RequestMapping("/eduVod/video")
@CrossOrigin
public class VodController {

    @Resource
    private VodService vodService;

    @ApiOperation("上传视频")
    @PostMapping("/uploadVideo")
    public Result<Map<String, String>> uploadVideo(MultipartFile file){
        String videoID = vodService.uploadVideo(file);
        Map<String, String> map = new HashMap<>();
        map.put("videoID",videoID);
        return ResultUtil.success(map);
    }


    @ApiOperation("删除视频")
    @DeleteMapping("/deleteVideo/{id}")
    public Result<String> deleteVideo(@PathVariable String id){
        vodService.deleteVideo(id);
        return ResultUtil.success();
    }

    @ApiOperation("删除多个视频")
    @DeleteMapping("/deleteBatch")
    public Result<String> deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.deleteBatch(videoIdList);
        return ResultUtil.success();
    }

    @ApiOperation("根据视频id获取视频凭证")
    @GetMapping("/getPlayAuth/{id}")
    public Result<Map<String,String>> getPlayAuth(@PathVariable String id){
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            Map<String,String> map = new HashMap<>();
            map.put("playAuth",playAuth);
            return ResultUtil.success(map);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }

    }


}
